﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;
using Model;//Please add references
namespace DAL
{
    /// <summary>
    /// 数据访问类:REDP_HEAD2
    /// </summary>
    public partial class REDP_HEAD2DAO
    {
        public REDP_HEAD2DAO()
        { }
        #region  BasicMethod


        public bool UpdateByOrderId(string tradeid, string orderid)
        {
            string sql = string.Format(@"Update REDP_HEAD2 set STATUS='支付成功',Trade = '{0}',TRANSDATE = getdate()  where OrderId = '{1}'", tradeid, orderid);

            int rows = DbHelperSQL.ExecuteSql(sql);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public DataSet GetName(int Id)
        {
            string str = string.Format("select Name from REDP_DETAIL2 where PID = {0}", Id);
            return DbHelperSQL.Query(str);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(REDP_HEAD2 model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into REDP_HEAD2(");
            strSql.Append("TRANSDATE,STORE,MEMBERNO,AddressID,PayType,Money,STATUS,OrderId,ShowUri)");
            strSql.Append(" values (");
            strSql.Append("@TRANSDATE,@STORE,@MEMBERNO,@AddressID,@PayType,@Money,@STATUS,@OrderId,@ShowUri)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@TRANSDATE", SqlDbType.DateTime),
					new SqlParameter("@STORE", SqlDbType.Int,4),
					new SqlParameter("@MEMBERNO", SqlDbType.VarChar,50),
					new SqlParameter("@AddressID", SqlDbType.Int,4),
					new SqlParameter("@PayType", SqlDbType.VarChar,50),
					new SqlParameter("@Money", SqlDbType.VarChar,50),
					new SqlParameter("@STATUS", SqlDbType.VarChar,50),
                    new SqlParameter("@OrderId",SqlDbType.VarChar,50),
                    new SqlParameter("@ShowUri", SqlDbType.VarChar,500)};
            parameters[0].Value = model.TRANSDATE;
            parameters[1].Value = model.STORE;
            parameters[2].Value = model.MEMBERNO;
            parameters[3].Value = model.AddressID;
            parameters[4].Value = model.PayType;
            parameters[5].Value = model.Money;
            parameters[6].Value = model.STATUS;
            parameters[7].Value = model.OrderId;
            parameters[8].Value = model.SHOWURI;
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(REDP_HEAD2 model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update REDP_HEAD2 set ");
            strSql.Append("TRANSDATE=@TRANSDATE,");
            strSql.Append("STORE=@STORE,");
            strSql.Append("MEMBERNO=@MEMBERNO,");
            strSql.Append("AddressID=@AddressID,");
            strSql.Append("PayType=@PayType,");
            strSql.Append("Money=@Money,");
            strSql.Append("STATUS=@STATUS");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@TRANSDATE", SqlDbType.DateTime),
					new SqlParameter("@STORE", SqlDbType.Int,4),
					new SqlParameter("@MEMBERNO", SqlDbType.VarChar,50),
					new SqlParameter("@AddressID", SqlDbType.Int,4),
					new SqlParameter("@PayType", SqlDbType.VarChar,50),
					new SqlParameter("@Money", SqlDbType.VarChar,50),
					new SqlParameter("@STATUS", SqlDbType.VarChar,50),
					new SqlParameter("@ID", SqlDbType.Int,4)};
            parameters[0].Value = model.TRANSDATE;
            parameters[1].Value = model.STORE;
            parameters[2].Value = model.MEMBERNO;
            parameters[3].Value = model.AddressID;
            parameters[4].Value = model.PayType;
            parameters[5].Value = model.Money;
            parameters[6].Value = model.STATUS;
            parameters[7].Value = model.ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from REDP_HEAD2 ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from REDP_HEAD2 ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public REDP_HEAD2 GetModel(int ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ID,TRANSDATE,STORE,MEMBERNO,AddressID,PayType,Money,STATUS from REDP_HEAD2 ");
            strSql.Append(" where ID=@ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
            parameters[0].Value = ID;

            REDP_HEAD2 model = new REDP_HEAD2();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public REDP_HEAD2 DataRowToModel(DataRow row)
        {
            REDP_HEAD2 model = new REDP_HEAD2();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["TRANSDATE"] != null && row["TRANSDATE"].ToString() != "")
                {
                    model.TRANSDATE = DateTime.Parse(row["TRANSDATE"].ToString());
                }
                if (row["STORE"] != null && row["STORE"].ToString() != "")
                {
                    model.STORE = int.Parse(row["STORE"].ToString());
                }
                if (row["MEMBERNO"] != null)
                {
                    model.MEMBERNO = row["MEMBERNO"].ToString();
                }
                if (row["AddressID"] != null && row["AddressID"].ToString() != "")
                {
                    model.AddressID = int.Parse(row["AddressID"].ToString());
                }
                if (row["PayType"] != null)
                {
                    model.PayType = row["PayType"].ToString();
                }
                if (row["Money"] != null)
                {
                    model.Money = row["Money"].ToString();
                }
                if (row["STATUS"] != null)
                {
                    model.STATUS = row["STATUS"].ToString();
                }
                if (row["OrderId"] != null)
                {
                    model.OrderId = row["OrderId"].ToString();
                }
                if (row["ShowUri"] != null)
                {
                    model.SHOWURI = row["ShowUri"].ToString();
                }
                if (row["TradeId"] != null)
                {
                    model.TradeId = row["TradeId"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM REDP_HEAD2 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,TRANSDATE,STORE,MEMBERNO,AddressID,PayType,Money,STATUS ");
            strSql.Append(" FROM REDP_HEAD2 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM REDP_HEAD2 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.ID desc");
            }
            strSql.Append(")AS Row, T.*  from REDP_HEAD2 T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "REDP_HEAD2";
            parameters[1].Value = "ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

